#!/usr/bin/python
from datetime import datetime
from itertools import islice
import sys
import time
import string

FMT = '%H:%M:%S'

currentsum = 0.0
filename=sys.argv[1]
tmpmah = 0
capacity = 0
ocvtable=[]
fd = open(filename, 'r')
lines = fd.readlines()
fd.close()

f = open(filename, 'r')

def totaltime():
	begintime=lines[0].split()[0].split()
	endtime=lines[len(lines)-1].split()[0].split()
	beginstr=string.join(begintime)
	endstr=string.join(endtime)
	totaltime = datetime.strptime(endstr, FMT) - datetime.strptime(beginstr, FMT)
#    print "Charged in %d hours"%totaltime.seconds
	return totaltime
	

def totalmah():
	totalvalue=0.0
	while 1:
		line = f.readline()
		if not line:
			break
		current = line.split()[3].split('=')[1]
		time1 = line.split()[0].split()
		time1str = string.join(time1)

		line2 = f.readline()
		if not line2:
			break
		time2 = line2.split()[0].split()
		time2str = string.join(time2)
		length2 = len(line2)
		seekvalue = -length2
		
		
		tdelta = datetime.strptime(time2str, FMT) - datetime.strptime(time1str, FMT)
		f.seek(seekvalue, 1)
	#	print current
	#       mas = float(current)*(1.0/interval)
		totalvalue+=int(current)*(tdelta.seconds)/(1000.0)
	mAh= totalvalue/(3600)
	f.seek(0,0)
	return mAh

	


def average(numlist):
	sumval=0
	for i in range(1,3):
		maxvol=max(numlist)
		minvol=min(numlist)
#   print(maxvol,minvol)
		numlist.remove(maxvol)
		numlist.remove(minvol)
	length=len(numlist)
	for i in numlist:
		sumval=sumval+i
	avervalue=sumval/length
	return avervalue

def firstsample():
	samplelist=[]
	for i in range(0,21):
		samplelist.append(int(lines[i].split()[2].split('=')[1]))
	return average(samplelist)


def lastsample():
	global lines
	samplelist=[]
	lastindex=len(lines)-1
	for i in range(0,21):
		samplelist.append(int(lines[lastindex-i].split()[2].split('=')[1]))
		#samplelist.append(int(lines[lastindex-i].split()[2].split('=')[1]))
	return average(samplelist)


def aversamples(sampline):
	global lines
#	print sampline
	for s in lines:
		if sampline in str(s):
			indexvalue = lines.index(s)
	return indexvalue

def getocv():
	global ocvtable
	global currentsum
	global capacity
	global lines
	while 1:
		line = f.readline()
		if not line:
			break
		current = line.split()[3].split('=')[1]
		time1 = line.split()[0].split()
		time1str = string.join(time1)

		line2 = f.readline()
		if not line2:
			break
		time2 = line2.split()[0].split()
		time2str = string.join(time2)
		length2 = len(line2)
		seekvalue = -length2
		
		
		tdelta = datetime.strptime(time2str, FMT) - datetime.strptime(time1str, FMT)
		f.seek(seekvalue, 1)
	#	print current
	#       mas = float(current)*(1.0/interval)
		currentsum+=int(current)*(tdelta.seconds)/(1000.0)
		tmpmah = abs(currentsum/3600) - capacity*percentage
		if (tmpmah/percentage > 1 and capacity < 99):
			capacity+=1
#			print "Capacity:%d%%"%capacity, line
			index = aversamples(line)
			voltagelist = []
			for j in range(-40, 41):
				sample = lines[index+j]
#				print sample
				voltagesample = sample.split()[2].split('=')[1]
				voltagelist.append(int(voltagesample))
			voltageocv = average(voltagelist)
#			print 'voltageocv:', voltageocv
			ocvtable.append(voltageocv)
				
def getocvtable():
	global ocvtable
	ocvvalue=[]
	for i in range(len(ocvtable)):
		value=int(round(ocvtable[i]/1000.0))
#		print value
		ocvvalue.append(value)
	ocvlen=len(ocvvalue)
	ocvvalue.reverse()
	print "OCV Table:"
	for y in range(ocvlen/10+1):
		for x in range(0,10):
			if (y*10+x < ocvlen):
				print "%d"%ocvvalue[y*10+x],
		print ''

total=totalmah()
deltatime=totaltime()
if (total > 0):
       print "Charged %dmAh in"%total, deltatime
else:
       print "Discharged %dmAh in"%total, deltatime

if len(sys.argv) == 3:
	percentage=int(sys.argv[2])/100
else:
	percentage=abs(total/100)

print "percentage:%dmAh"%percentage
firstocv=firstsample()
ocvtable.append(firstocv)
getocv()
#print 'ocvtable:', ocvtable
lastocv=lastsample()
ocvtable.append(lastocv)
getocvtable()
f.close()
#print 'Current mas:' ,currentsum


